package com.example.springboot.mapper;

import com.example.springboot.entity.Post;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface PostMapper {
    /*
    id,content, author_id as authorId, category_id as categoryId, tags, updated_time as updatedTime, published_time as publishedTime, status, views, likes, comments_count as commentsCount, collect_count as collectCount, article_type as articleType, cover, title,summary
    #{content}, #{authorId}, #{categoryId}, #{tags}, #{updatedTime}, #{publishedTime}, #{status}, #{views}, #{likes}, #{commentsCount}, #{collectCount}, #{articleType}, #{cover}, #{title}),#{summary}
     */
    @Select("select id,content, author_id as authorId, category_id as categoryId, tags, updated_time as updatedTime, published_time as publishedTime, status, views, likes, comments_count as commentsCount, collect_count as collectCount, article_type as articleType, cover, title,summary from blog_posts LIMIT #{pageSize} OFFSET #{offset}")
    List<Post> findAll(Integer offset, Integer pageSize);

    @Insert("INSERT INTO blog_posts (content, author_id, category_id, tags, updated_time, published_time, status, views, likes, comments_count, collect_count, article_type, cover, title, summary) " +
            "VALUES (#{content}, #{authorId}, #{categoryId}, #{tags}, #{updatedTime}, #{publishedTime}, #{status}, #{views}, #{likes}, #{commentsCount}, #{collectCount}, #{articleType}, #{cover}, #{title}, #{summary})")
    void insert(Post post);


    @Select("select id,content, author_id as authorId, category_id as categoryId, tags, updated_time as updatedTime, published_time as publishedTime, status, views, likes, comments_count as commentsCount, collect_count as collectCount, article_type as articleType, cover, title,summary from blog_posts " +
            "where id = #{postId}")
    Post findOneById(Integer postId);

    List<Post> findAllByCategoryId(Integer offset, Integer pageSize, Integer categoryId);

    List<Post> getRankingsByLikes(Integer offset, Integer pageSize);

    List<Post> getRankingsByViews(Integer offset, Integer pageSize);

    List<Post> getRankingsByCollectCount(Integer offset, Integer pageSize);

    void increCollectCount(Integer collectedId);

    void decreCollectCount(Integer collectedId);

    void increLikes(Integer likedId);

    void decreLikes(Integer likedId);

    void increViews(Integer id);
}
